package com.wuliner.unlock_new

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment

class registePIN:Fragment() {
    lateinit var user:User
    //输入的用户名
   lateinit var name:String
    //输入新密码
    lateinit var namePsd:String
    //确定密码
    lateinit var secondPwd:String
    lateinit var main :MainActivity
    lateinit var firstPwdView:EditText
    lateinit var secondPwdView:EditText
    @SuppressLint("ResourceType")
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        this.name = main.findViewById<EditText>(R.id.editTextName).toString()
        val register = main.findViewById<Button>(R.id.button)
        register.setOnClickListener { //存储用户的条件
        fun isAddUser(){
            if (isNameExist(this.name) == null){

                inputSecondPwd(inputFirstPwd())
                if (namePsd == secondPwd){
                    main.findViewById<TextView>(R.id.textViewed).text = "输入正确"
                    firstPwdView.setTextColor(Color.GREEN)
                    secondPwdView.setTextColor(Color.GREEN)
                    Handler().postDelayed({
                        User.storeNum(this.name,namePsd)
                    },1000)

                }else{
                    firstPwdView.setTextColor(Color.RED)
                    secondPwdView.setTextColor(Color.RED)
                    main.findViewById<TextView>(R.id.textViewed).text = "输入错误，两次密码不一致"
                    Handler().postDelayed({
                       firstPwdView.text.clear()
                        secondPwdView.text.clear()
                    },1000)
                }
            }
        }
        }


        return inflater.inflate(R.layout.fragment_registe,container,true)

    }
    //输入用户名，判断用户名是否存在及这一栏的工作
    fun isNameExist(name: String):String ? {
        if(User.loadUserInfo(name) != null){
            val userPersonText = main.findViewById<TextView>(R.id.textView)
            userPersonText.text = "用户已存在"
           userPersonText.setTextColor(Color.RED)
            return null
        }else{
            return name
        }
    }
    //第一次输入密码
    fun inputFirstPwd():String{
         firstPwdView  = main.findViewById<EditText>(R.id.editTextpwdst)
        namePsd = firstPwdView.text.toString()
        return namePsd
    }
    //第二次输入密码
    fun inputSecondPwd (namePsd:String):String{
        secondPwdView= main.findViewById<EditText>(R.id.editTextNamepwd)
        secondPwd = secondPwdView.text.toString()
        return secondPwd
    }
}